import React, { Component } from 'react';
import { Form, Row, Col } from 'antd';
import ShowType from '../../../routes/common/showType';
import styles from '../../../index.less';
import * as config from '../../../utils/config';

const FormItem = Form.Item;

class TabMainComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      sId: props.sId, /* 业务数据Id */
      dataList: [], /* 数据 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      tCreateDate: '', /* 制单日期 */
      sMakePerson: '', /* 制单人 */
      bInvalid: '', /* 是否作废 */
      sName: '', /* 名称 */
      sAllPartsName: '', /* 所有部件名称 */
      sType: '', /* 类型 */
    };
  }

  componentWillMount() {
    /* 获取主表数据 */
    const {
      masterConfig, sActiveId, sId, componentName, sTabId,
    } = this.state;
    const { dispatch } = this.props;
    if (sId !== undefined && sId !== '') {
      const mainId = masterConfig.sId;
      /* 表Id */
      const value = { sId, pageSize: '', pageNum: '' };
      /* 业务数据Id */
      const dataUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${mainId}?sModelsId=${sActiveId}`;
      dispatch({
        type: 'content/getUrlPostData',
        payload: {
          url: dataUrl, sTabId, componentName, value, formRoute: '/sisproductclassify', returnType: 'data',
        },
      });
      /* 获取主表数据 */
    } else if (sId === '' && this.props.newIdFlag) {
      const url = `${config.server_host}getUuid/${this.props.newIdlength}`;
      dispatch({
        type: 'content/getCopyDataId',
        payload: {
          sTabId, url, formRoute: '/sisproductclassify', componentName,
        },
      });
    }
    if (masterConfig.gdsconfigformslave !== undefined) {
      const mainConfig = masterConfig.gdsconfigformslave;
      this.setState({
        sId: sId !== undefined ? sId : '',
        tCreateDate: mainConfig.filter(item => (item.sName.toLowerCase() === 'tcreatedate'))[0],
        sMakePerson: mainConfig.filter(item => (item.sName.toLowerCase() === 'smakeperson'))[0],
        bInvalid: mainConfig.filter(item => (item.sName.toLowerCase() === 'binvalid'))[0],
        sName: mainConfig.filter(item => (item.sName.toLowerCase() === 'sname'))[0],
        sAllPartsName: mainConfig.filter(item => (item.sName.toLowerCase() === 'sallpartsname'))[0],
        sType: mainConfig.filter(item => (item.sName.toLowerCase() === 'stype'))[0],
      });
    }
  }

  componentWillReceiveProps(nextProps) {
    const {
      sId, disabled, content, dataSet,
    } = nextProps;
    const { dataList } = content;
    const {
      sDataListTabId, returnType, urlData, loading, componentName,
    } = dataList;
    this.setState({ disabled, dataList: dataSet });
    if (this.state.sTabId === sDataListTabId && this.state.sTabId === nextProps.app.currentPane.key
      && this.state.componentName === componentName) {
      if (loading) return;
      if (returnType === 'data') {
        this.setState({
          dataList: urlData,
          sId: sId !== undefined ? sId : '',
        });
      }
      this.setState({ sId });
    }
  }

  shouldComponentUpdate(nextProps, nextState) {
    const { componentName } = nextProps;
    const { sId } = this.props;
    if (sId === undefined) {
      return true;
    }
    let flag = true;
    if (nextState.disabled !== this.state.disabled) {
      return true;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key &&
      this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  handleToggle = () => {
    const { expand } = this.state;
    this.setState({ expand: !expand });
  };

  render() {
    const {
      app, form, handleModalPane, dataName, dataType, handleSelectChange, onClose,
    } = this.props;
    const {
      dataList, sId, sActiveId, disabled,
    } = this.state;
    const childProps = {
      app,
      form,
      sActiveId,
      dataList,
      sId,
      disabled,
      handleModalPane,
      handleSelectChange,
      dataName,
      dataType,
      onClose,
    };
    const {
      tCreateDate, sMakePerson, bInvalid, sName, sAllPartsName, sType,
    } = this.state;
    const tCreateDateProps = { showConfig: tCreateDate, ...childProps };
    const sMakePersonProps = { showConfig: sMakePerson, ...childProps };
    const bInvalidProps = { showConfig: bInvalid, ...childProps };
    const sNameProps = { showConfig: sName, ...childProps };
    const sAllPartsNameProps = { showConfig: sAllPartsName, ...childProps };
    const sTypeProps = { showConfig: sType, ...childProps };
    return (
      <FormItem className={styles.searchMainForm}>
        <Row type="flex">
          <Col
            span={6}
            order={tCreateDate.iOrder > 24 ? 24 : tCreateDate.iOrder}
            style={{ display: tCreateDate.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="tCreateDate" showDefaultName="制单日期" {...tCreateDateProps} />
          </Col>
          <Col
            span={6}
            order={sMakePerson.iOrder > 24 ? 24 : sMakePerson.iOrder}
            style={{ display: sMakePerson.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sMakePerson" showDefaultName="制单人" {...sMakePersonProps} />
          </Col>
          <Col
            span={6}
            order={bInvalid.iOrder > 24 ? 24 : bInvalid.iOrder}
            style={{ display: bInvalid.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="bInvalid" showDefaultName="是否作废" {...bInvalidProps} />
          </Col>
          <Col
            span={6}
            order={sName.iOrder > 24 ? 24 : sName.iOrder}
            style={{ display: sName.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sName" showDefaultName="名称" {...sNameProps} />
          </Col>
          <Col
            span={6}
            order={sAllPartsName.iOrder > 24 ? 24 : sAllPartsName.iOrder}
            style={{ display: sAllPartsName.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sAllPartsName" showDefaultName="所有部件名称" {...sAllPartsNameProps} />
          </Col>
          <Col
            span={6}
            order={sType.iOrder > 24 ? 24 : sType.iOrder}
            style={{ display: sType.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sType" showDefaultName="类型" {...sTypeProps} />
          </Col>
        </Row>
      </FormItem>
    );
  }
}

export default TabMainComponent;
